package com.koron.configure.safetcf.controller;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import com.koron.common.core.util.StringUtils;
import com.koron.configure.enums.SortEnum;
import com.koron.configure.safetcf.bean.SafetCFyBean;
import com.koron.configure.safetcf.bean.SafetCFyDto;
import com.koron.configure.safetcf.mapper.SafetyCFBeanMapper;
import com.koron.configure.safetcf.service.imp.SafetCFImpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;


@Api(tags = "安全措施配置")
@RestController
@RequestMapping("/safetcf")
public class SafetCFController {

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    private EamUser getCurrUser() {
        return ThreadLocalContext.get();
    }

    /**
     * 服务对象
     */
    @Autowired
    private SafetCFImpService safetcfimpservice;


    @ApiOperation(value = "安全措施-列表")
    @GetMapping("queryList")
    public Response<List<SafetCFyDto>> queryList(@RequestParam(value = "ticketType") String ticketType) {
        List<SafetCFyDto> list = ADOConnection.runTask(getCurrDs(), factory -> factory.getMapper(SafetyCFBeanMapper.class).queryAllByTicketType(ticketType),List.class);
        return Response.ok(list);
    }


    @ApiOperation(value = "安全措施-保存、新增")
    @GetMapping("updatePrio")
    public Response<String> updatePrio(SafetCFyDto dto) {

        //获取字典里所有工作票类型
        Map<String, String> dicmap = CodeTools.getDictByClassCode( "twoTicketManagement", "ticket_work_type");
        Set<String> keySet = dicmap.keySet();
        Assert.isFalse(!keySet.contains(dto.getTicketType()),"没该工作票类型");



        //获取该工作票类型 的 安全措施数量
        Integer maxQty = ADOConnection.runTask(getCurrDs(), factory -> {
            Integer qty = factory.getMapper(SafetyCFBeanMapper.class).countByTicketType(dto.getTicketType());
            return qty;
        }, Integer.class);

        if (StringUtils.isNotEmpty(dto.getSortType())) {
            Assert.isFalse(StringUtils.isNull(dto.getSort()), "当前措施排序号不能为空");

            if (dto.getSortType().equals(SortEnum.UP.getValue())) {
                if (dto.getSort() != 1) {
                    ADOConnection.runTask(getCurrDs(), factory -> {
                        return factory.getMapper(SafetyCFBeanMapper.class).updateByTicketTypeAndSort(dto.getSort(), dto.getTicketType(), dto.getSort() - 1);
                    }, Integer.class);
                    dto.setSort(dto.getSort() - 1);
                }
            }
            if (dto.getSortType().equals(SortEnum.DOWN.getValue())) {
                if (dto.getSort() != maxQty) {
                    ADOConnection.runTask(getCurrDs(), factory -> {
                        return factory.getMapper(SafetyCFBeanMapper.class).updateByTicketTypeAndSort(dto.getSort(), dto.getTicketType(), dto.getSort() + 1);
                    }, Integer.class);
                    dto.setSort(dto.getSort() + 1);
                }
            }
        }

        if (StringUtils.isEmpty(dto.getId())) {
            dto.setSort(maxQty + 1);
        }
        if (maxQty == 0) {
            dto.setSort(1);
        }
        SafetCFyBean bean = Convert.convert(SafetCFyBean.class, dto);
//        String id = safetcfimpservice.savePrio(bean);
        String id = ADOConnection.runTask(getCurrDs(), safetcfimpservice, "savePrio", String.class, bean);
        return Response.ok("成功", id);
    }

    @ApiOperation(value = "安全措施-删除")
    @DeleteMapping("deleteById")
    public Response<String> deleteById(@RequestParam(value = "id") String id) {
        ADOConnection.runTask(getCurrDs(), factory -> {
            SafetyCFBeanMapper mapper = factory.getMapper(SafetyCFBeanMapper.class);
            SafetCFyBean safetCFyBean = mapper.selectByPrimaryKey(id);
            mapper.deleteByPrimaryKey(id);
            List<SafetCFyDto> list = mapper.queryAllByTicketType(safetCFyBean.getTicketType());
            List<SafetCFyBean> converlist = Convert.toList(SafetCFyBean.class, list);
            Stream.iterate(0, i->i+1).limit(converlist.size()).forEach(i->{
                SafetCFyBean bean = converlist.get(i);
                bean.setSort(i+1);
            });
            mapper.updateBatchSelective(converlist);
            return 1;
        },Integer.class);
        return Response.ok("成功");
    }

}
